import React, { Fragment, useState, useEffect } from 'react';
import { connect } from 'umi';
import { Form, Card, Button } from 'antd';
import { history } from 'umi';
import { eventTrack } from '@/utils/track';
import PageHeaderWrapper from '@/components/PageHeaderWrapper/index';
import PersonInfo from '@/components/PersonInfo';
import LinkTo from '@/components/LinkTo';
import SlideTabs from '@/components/SlideTabs';
import { withRoutePage } from '@/utils/enhanceUtils';
import PageRouter from '@/components/PageRouter/PageRouter';
import Resume from './DetailResume/Resume';
import ViewRecord from './DetailResume/ViewRecord';
// 修改状态色
const dictColor = {
    applying: 'blue', // 申请中
    apply_failed: 'red', // "申请未通过"
    apply_success: 'green', // 申请通过
    be_pending: 'blue', // 待定
    eliminated: 'red', // 淘汰
    entry_cancel: 'red', // 取消入职
    entry_success: 'green', // 已入职
    offer_accepted: 'blue', // 已接受
    offer_refused: 'red', // 已拒绝
    offer_sended: 'blue', // 已发送
    pass: 'green', // 通过
    wait_for_beginning: '', // "待开始"
    wait_for_processing: '', // 待处理
    entry_doing: 'blue' // 入职中"
};
const Detail = ({ dispatch, location, common, filterRecord, route }) => {
    const {
        query: { candidateCode, isApplyOffer = 'false', isInterview = 'false' }
    } = location;
    const { path } = route;
    let typeJump = '';
    if (path === '/office/personnelManagement/recruit/candidate/detail') {
        typeJump = 'company';
    } else if (path === '/personal/portal/recruit/candidate/detail') {
        typeJump = 'mine';
    } else {
        throw new Error('未找到路径！请检查');
    }
    const { candiDetail = {}, asianList = [] } = common;
    const { interviewList = {} } = filterRecord; // 面试记录表格数据
    const {
        talentName = '',
        recruitPost = '',
        candidateState = '',
        candidateStageValue = '',
        candidateStateValue = '',
        talentSex = '',
        talentLeaderName = ''
    } = candiDetail;
    const [activeKey, setActiveKey] = useState('1');
    const tabs = [
        {
            label: '简历信息',
            key: '1'
        },
        {
            label: '面试记录',
            key: '2'
        }
    ];
    // 发起offer
    const getOffer = () => {
        dispatch({
            type: 'common/featchApply',
            payload: {
                companyId: candiDetail.companyId || '' // 候选人公司id
            },
            callback: res => {
                history.push({
                    pathname: '/personal/portal/flow/mine/list/apply',
                    query: {
                        action: 'edit',
                        formDeploymentId: res.formDeploymentId,
                        processDeploymentId: res.processDeploymentId,
                        templateId: res.id,
                        templateName: res.templateName, // 后台返回值
                        talentId: candiDetail.candidateCode || '',
                        talentName
                    }
                });
            }
        });
    };
    // 判断页面按钮权限
    const buttonSetting = () => {
        const newArray = [];

        if (isInterview === 'true') {
            newArray.push(
                <Button type="primary" ghost>
                    <LinkTo
                        onClick={async (e, url) => {
                            await eventTrack({
                                eventName: 'personnel_management_recruit_candidate_detail_interview_add_click',
                                eventProps: {
                                    label: '候选人详情-发起面试'
                                }
                            });
                            history.push(url);
                        }}
                        to={
                            typeJump === 'mine'
                                ? '/personal/portal/recruit/interview/add'
                                : '/office/personnelManagement/recruit/interview/add'
                        }
                        query={{
                            candidateCode,
                            type: 'type0'
                        }}
                        className="marginR"
                    >
                        发起面试
                    </LinkTo>
                </Button>
            );
        }
        if (isApplyOffer === 'true') {
            newArray.push(
                <Button
                    type="primary"
                    onClick={e => {
                        e.preventDefault();
                        // console.log(2);
                        getOffer();
                    }}
                    ghost
                >
                    申请offer
                </Button>
            );
        }
        return newArray;
    };
    const getSexName = key => {
        if (key === '1') {
            return 'man';
        }
        if (key === '2') {
            return 'woman';
        }
        return '';
    };
    useEffect(() => {
        // 获取候选人详情-简历信息
        dispatch({
            type: 'common/candidateDetail',
            payload: {
                candidateCode
            }
        });
        // 获取候选人详情-面试记录
        dispatch({
            type: 'filterRecord/fetchInterviewRecord',
            payload: {
                candidateCode
            }
        });
        // 籍贯下拉项
        dispatch({
            type: 'common/fetchAsisn',
            payload: {
                dictCode: 'asian'
            }
        });
        // 清空候选人详情数据
        return () => {
            dispatch({
                type: 'common/save',
                payload: { candiDetail: {} }
            });
        };
    }, []);
    return (
        <PageHeaderWrapper type="success">
            <PageRouter type="myrecruit" activePath="/personal/portal/recruit/candidate">
                <Card bordered={false} style={{ padding: '24px 0' }} bodyStyle={{ padding: 0 }}>
                    <PersonInfo
                        style={{ padding: '0 24px' }}
                        name={`${talentName}-${recruitPost}`}
                        status={`${candidateStageValue}-${candidateStateValue}`}
                        sex={getSexName(talentSex)}
                        color={dictColor[candidateState]}
                        infos={[
                            {
                                label: '招聘负责人',
                                value: talentLeaderName
                            }
                        ]}
                        btns={buttonSetting()}
                    />
                    <SlideTabs
                        tabs={tabs}
                        activeKey={activeKey}
                        onChange={key => setActiveKey(key)}
                        cardStyle={{
                            height: 'calc(100vh - 400px)',
                            marginRight: 4,
                            padding: 24
                        }}
                    >
                        {refs => (
                            <Fragment>
                                <div ref={refs[0].ref}>
                                    <Resume
                                        taletData={candiDetail}
                                        asian={asianList}
                                        readonly="readonly"
                                        disabled="true"
                                    />
                                </div>
                                <div
                                    style={{
                                        height: 1,
                                        background: 'rgba(232,232,232,1)',
                                        margin: '15px 0'
                                    }}
                                />
                                <div ref={refs[1].ref}>
                                    <div className="vv-from-title">面试记录</div>
                                    <ViewRecord tableData={interviewList} companyId={candiDetail.companyId} />
                                </div>
                            </Fragment>
                        )}
                    </SlideTabs>
                </Card>
            </PageRouter>
        </PageHeaderWrapper>
    );
};

// export default Detail;
export default withRoutePage(
    connect(({ common, filterRecord, loading }) => ({
        common,
        filterRecord,
        loading
    }))(Form.create()(Detail))
);
